package com.cssw.fyzb.busi.api.task;

import com.cssw.fyzb.busi.convert.Converter;
import com.cssw.fyzb.core.model.vo.task.TaskVo;
import com.cssw.fyzb.busi.service.task.TaskService;
import com.cssw.fyzb.core.model.dodb.Task;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
@Function(description = "历史任务", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "pageNumber", type = Type.Integer, description = "当前页，从1开始", required = true),
        @Parameter(name = "pageSize", type = Type.Integer, description = "页大小", required = true),
})
@Component
public class OldTask implements Action {

    private final static Logger logger = LoggerFactory.getLogger(OldTask.class);

    @Autowired
    private TaskService taskService;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        Integer pageNumber = request.getInteger("pageNumber");
        Integer pageSize = request.getInteger("pageSize");
        try {
            Page<Task> page = taskService.oldTask(userId, pageNumber, pageSize);
            List<TaskVo> list = ((Converter<List<Task>, List<TaskVo>>) videos -> {
                List<TaskVo> vos = new ArrayList<>();
                videos.forEach((v) -> {
                    TaskVo vo = new TaskVo();
                    vo.setAllValue(v.getAllValue());
                    vo.setCycleDay(v.getCycleDay());
                    vo.setLimitNum(v.getLimitNum());
                    vo.setPrice(v.getPrice());
                    vo.setProduceDay(v.getProduceDay());
                    vo.setTaskCond(v.getTaskCond());
                    vo.setTaskId(v.getTaskId());
                    vo.setTaskName(v.getTaskName());
                    vo.setTaskTime(v.getTaskTime());
                    vo.setVitality(v.getVitality());
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    if (v.getStr("create_time") != null) {
                        vo.setCreateTime(format.format(v.getDate("create_time")));
                    }
                    //求当前任务已完成进度 100 -（剩余产值 除 总产值）
                    vo.setToComplete(new BigDecimal(100).subtract
                            (v.getBigDecimal("surplus_value").divide(v.getBigDecimal("all_value"), 3, BigDecimal.ROUND_HALF_UP)
                                    .multiply(new BigDecimal(100))));
                  //  vo.setLncomeRatio(v.getAllValue().divide(v.getPrice()).subtract(new BigDecimal(1)).multiply(new BigDecimal(100)));
                    vo.setTaskIcon(v.getStr("task_icon"));
                    vos.add(vo);
                });
                return vos;
            }).convert(page.getList());
            return Response.success().put("list", list).put("total", page.getTotalPage()).put("count", page.getTotalRow());
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("查询失败");
        }
    }
}
